package vip.xiaonuo.smzq.modular.dictionary.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import vip.xiaonuo.smzq.modular.dictionary.entity.*;
import vip.xiaonuo.smzq.modular.dictionary.entity.vo.Cb06KcVo;
import vip.xiaonuo.smzq.modular.dictionary.entity.vo.KcNameTree;
import vip.xiaonuo.smzq.modular.dictionary.entity.vo.ZhhskcTree;
import vip.xiaonuo.smzq.modular.dictionary.mapper.*;
import vip.xiaonuo.smzq.modular.dictionary.service.ICb06KcService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 矿产 服务实现类
 *
 * @author eomer
 * @since 2024-02-21
 */
@Service
public class Cb06KcServiceImpl extends ServiceImpl<Cb06KcMapper, Cb06Kc> implements ICb06KcService {

    @Resource
    private Cb06KcMapper cb06KcMapper;
    @Resource
    private Cb01ZhMapper cb01ZhMapper;
    @Resource
    private Cb02KslxMapper cb02KslxMapper;//矿石类型
    @Resource
    private Cb03KspjMapper cb03KspjMapper;//矿石品级
    @Resource
    private Cb04KszlMapper cb04KszlMapper;//矿石种类

    @Override
    public Page<Cb06Kc> page(Cb06Kc cb06Kc) {
        QueryWrapper<Cb06Kc> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    /**
     * 矿产名称下拉框
     */
    @Override
    public KcNameTree getKcNameTree() {
        return getTreeForKcName();
//        KcNameTree kcNameTree = new KcNameTree();
//        kcNameTree.setName("矿产名称");
//        kcNameTree.setId("kcmc");
//        kcNameTree.setKcdm(0);
//        kcNameTree.setKcmcdm("矿产名称");
//        try {
//            List<KcNameTree> kcNameTreeList = new ArrayList<>();
//            //获取矿类表
//            List<Cb01Zh> cb01ZhList = new ArrayList<>();
//            QueryWrapper<Cb01Zh> query = new QueryWrapper<>();
//            query.eq("bh", 70);
//            query.ne("bnm", 0);
//            cb01ZhList = cb01ZhMapper.selectList(query);
//            for (Cb01Zh cb01Zh : cb01ZhList) {
//                KcNameTree kcNameTree1 = new KcNameTree();
//                kcNameTree1.setPid("kcmc");
//                kcNameTree1.setId(cb01Zh.getPkid());
//                kcNameTree1.setName(cb01Zh.getMc());
//                kcNameTree1.setKcdm(cb01Zh.getDm());
//                kcNameTree1.setKcmcdm(cb01Zh.getMc());
//                kcNameTree1.setChildren(getKcNameTreeList(cb01Zh.getDm()));
//                kcNameTreeList.add(kcNameTree1);
//            }
//            kcNameTree.setChildren(kcNameTreeList);
//        } catch (Exception e) {
//            System.out.println(e);
//        }
//        return kcNameTree;
    }

    /**
     * 综合回收矿产名称下拉框
     */
    @Override
    public ZhhskcTree getZhhskcTree() {
        ZhhskcTree kcNameTree = new ZhhskcTree();
        kcNameTree.setName("矿产名称");
        kcNameTree.setId("kcmc");
        kcNameTree.setKcdm(0);
        kcNameTree.setKcmcdm("矿产名称");
        try {
            List<ZhhskcTree> kcNameTreeList = new ArrayList<>();
            //获取矿类表
            List<Cb01Zh> cb01ZhList = new ArrayList<>();
            QueryWrapper<Cb01Zh> query = new QueryWrapper<>();
            query.eq("bh", 70);
            query.ne("bnm", 0);
            cb01ZhList = cb01ZhMapper.selectList(query);
            for (Cb01Zh cb01Zh : cb01ZhList) {
                ZhhskcTree kcNameTree1 = new ZhhskcTree();
                kcNameTree1.setPid("kcmc");
                kcNameTree1.setId(cb01Zh.getPkid());
                kcNameTree1.setName(cb01Zh.getMc());
                kcNameTree1.setKcdm(cb01Zh.getDm());
                kcNameTree1.setKcmcdm(cb01Zh.getMc());
                kcNameTree1.setChildren(getZhhsKcNameTreeList(cb01Zh.getDm()));
                kcNameTreeList.add(kcNameTree1);
            }
            kcNameTree.setChildren(kcNameTreeList);
        } catch (Exception e) {
            System.out.println(e);
        }
        return kcNameTree;
    }

    //综合描述矿产种类下的节点
    public List<ZhhskcTree> getZhhsKcNameTreeList(Integer dm) {
        List<ZhhskcTree> kcNameTreeList = new ArrayList<>();

        List<String> kcnames = cb06KcMapper.selectKcNames(String.valueOf(dm));
        for (String kcName : kcnames) {
            QueryWrapper<Cb06Kc> query = new QueryWrapper<>();
            query.eq("kcmc", kcName);
            List<Cb06Kc> cb06KcList = cb06KcMapper.selectList(query);
            if (cb06KcList != null) {
                Cb06Kc cb06Kc = cb06KcList.get(0);
                ZhhskcTree kcNameTree1 = new ZhhskcTree();
                kcNameTree1.setPid(String.valueOf(dm));
                kcNameTree1.setName(cb06Kc.getKcmc());
                kcNameTree1.setKcdm(cb06Kc.getKcdm());
                kcNameTree1.setKcmcdm(cb06Kc.getKcmc() + " " + cb06Kc.getKcdm());
                kcNameTree1.setKcdm(cb06Kc.getKcdm());
                kcNameTree1.setKsldw(cb06Kc.getKsldw());
                if (cb06Kc.getJsldw() != null){
                    kcNameTree1.setJsldw(cb06Kc.getJsldw());
                }else{
                    kcNameTree1.setJsldw("");
                }
                kcNameTree1.setId(cb06Kc.getPkid());
                //矿石质量筛选下拉框
                kcNameTree1.setCb04KszlList(getKszlList(cb06Kc.getKcdm()));
                kcNameTreeList.add(kcNameTree1);
            }
        }
        return kcNameTreeList;
    }

    /**
     * 综合回收矿产名称下拉框-重构
     *
     * @author Kevin
     * @since 2024/7/24 17:18
     */
    @Override
    public ZhhskcTree getZhhskcTreeList() {
        ZhhskcTree kcNameTree = new ZhhskcTree();
        kcNameTree.setName("矿产名称");
        kcNameTree.setId("kcmc");
        kcNameTree.setKcdm(0);
        kcNameTree.setKcmcdm("矿产名称");

        //查询全部矿产品与其对应的矿石质量
        List<Cb04Kszl> kszlList = cb04KszlMapper.selectList(new QueryWrapper<>());

        //根据矿产代码分组
        Map<String, List<Cb04Kszl>> kszlGroupMap = kszlList.stream().collect(Collectors.groupingBy(o -> String.valueOf(o.getKcdm())));

        //查询综合回收矿产
        List<Cb06KcVo> zhhsList = cb06KcMapper.selectZhhsList();

        //根据综合回收代码分组-剔除kcmc为空的数据，避免下面继续根据kcmc分组时空指针
        Map<String, List<Cb06KcVo>> zhhsGroupMap = zhhsList.stream().filter(o -> StringUtils.isNotBlank(o.getKcmc())).collect(Collectors.groupingBy(o -> o.getDm()));

        //单独过滤出kcmc为空的数据组成List
        List<Cb06KcVo> kcmcNullList = zhhsList.stream().filter(o -> StringUtils.isBlank(o.getKcmc())).collect(Collectors.toList());

        //遍历分组
        List<ZhhskcTree> kcNameTreeList = new ArrayList<>();
        for (Map.Entry<String, List<Cb06KcVo>> entry : zhhsGroupMap.entrySet()) {
            List<Cb06KcVo> cpList = entry.getValue();
            String dm = entry.getKey();

            ZhhskcTree treeNode = new ZhhskcTree();
            treeNode.setPid("kcmc");
            treeNode.setKcdm(Integer.parseInt(dm));
            if (CollectionUtil.isNotEmpty(cpList)) {
                Cb06KcVo vo = cpList.get(0);
                treeNode.setId(vo.getId());
                treeNode.setName(vo.getMc());
                treeNode.setKcmcdm(vo.getMc());
            }

            //组织子集
            if (CollectionUtil.isNotEmpty(cpList)) {

                //子集继续根据kcmc分组
                Map<String, List<Cb06KcVo>> childGroupMap = cpList.stream().collect(Collectors.groupingBy(o -> o.getKcmc()));

                //遍历子集分组
                List<ZhhskcTree> childTreeList = new ArrayList<>();
                for (Map.Entry<String, List<Cb06KcVo>> childEntry : childGroupMap.entrySet()) {
                    List<Cb06KcVo> childList = childEntry.getValue();
                    String kcmc = childEntry.getKey();

                    ZhhskcTree zhhskcTree = new ZhhskcTree();
                    zhhskcTree.setPid(dm);
                    zhhskcTree.setName(kcmc);
                    if (CollectionUtil.isNotEmpty(childList)) {
                        Cb06KcVo co = childList.get(0);
                        zhhskcTree.setKcdm(co.getKcdm());
                        zhhskcTree.setKcmcdm(kcmc + " " + co.getKcdm());
                        zhhskcTree.setKsldw(co.getKsldw());
                        zhhskcTree.setJsldw(co.getJsldw() != null ? co.getJsldw() : "");
                        zhhskcTree.setId(co.getPkid());
                        //矿石质量筛选下拉框
                        List<Cb04Kszl> kszls1 = kszlGroupMap.get(String.valueOf(co.getKcdm()));
                        if (kszls1 != null) {
                            zhhskcTree.setCb04KszlList(kszls1);
                        } else {
                            zhhskcTree.setCb04KszlList(new ArrayList<>());
                        }
                    }
                    childTreeList.add(zhhskcTree);
                }
                treeNode.setChildren(childTreeList);
            }
            kcNameTreeList.add(treeNode);
        }

        //追加kcmc为空的集合，无子集
        if (CollectionUtil.isNotEmpty(kcmcNullList)) {
            for (Cb06KcVo vo : kcmcNullList) {
                ZhhskcTree treeNode = new ZhhskcTree();
                treeNode.setPid("kcmc");
                treeNode.setKcdm(Integer.parseInt(vo.getDm()));
                treeNode.setId(vo.getId());
                treeNode.setName(vo.getMc());
                treeNode.setKcmcdm(vo.getMc());
                treeNode.setChildren(new ArrayList<>());
                kcNameTreeList.add(treeNode);
            }
        }
        kcNameTree.setChildren(kcNameTreeList);
        return kcNameTree;
    }

    //矿产种类下的节点
    public List<KcNameTree> getKcNameTreeList(Integer dm) {
        List<KcNameTree> kcNameTreeList = new ArrayList<>();

        List<String> kcnames = cb06KcMapper.selectKcNames(String.valueOf(dm));
        for (String kcName : kcnames) {
            QueryWrapper<Cb06Kc> query = new QueryWrapper<>();
            query.eq("kcmc", kcName);
            List<Cb06Kc> cb06KcList = cb06KcMapper.selectList(query);
            if (cb06KcList != null) {
                Cb06Kc cb06Kc = cb06KcList.get(0);
                KcNameTree kcNameTree1 = new KcNameTree();
                kcNameTree1.setPid(String.valueOf(dm));
                kcNameTree1.setName(cb06Kc.getKcmc());
                kcNameTree1.setKcdm(cb06Kc.getKcdm());
                kcNameTree1.setKcmcdm(cb06Kc.getKcmc() + " " + cb06Kc.getKcdm());
                if (cb06KcList.size() > 1) {
                    kcNameTree1.setId(cb06Kc.getKclb());
                    List<KcNameTree> kcNameTreeList2 = new ArrayList<>();
                    for (Cb06Kc cb06Kc1 : cb06KcList) {
                        KcNameTree kcNameTree2 = new KcNameTree();
                        kcNameTree2.setPid(cb06Kc1.getPkid());
                        kcNameTree2.setKcdm(cb06Kc1.getKcdm());
                        kcNameTree2.setKsldw(cb06Kc1.getKsldw());
                        // kcNameTree2.setKcmcdm(cb06Kc1.getKcmc()+" "+cb06Kc1.getKcdm()+" "+cb06Kc1.getJsdxmc());
                        kcNameTree2.setName(cb06Kc1.getJsdxmc() + " " + cb06Kc1.getKsldw());
                        //矿石类型筛选下拉
                        kcNameTree2.setCb02KslxList(getKslxList(cb06Kc1.getKcdm()));
                        //矿产品级筛选下拉
                        kcNameTree2.setCb03KspjList(kcpjList(cb06Kc1.getKcdm()));
                        //矿石质量筛选下拉框
                        kcNameTree2.setCb04KszlList(getKszlList(cb06Kc1.getKcdm()));

                        kcNameTreeList2.add(kcNameTree2);
                    }
                    kcNameTree1.setChildren(kcNameTreeList2);
                } else {
                    kcNameTree1.setKcdm(cb06Kc.getKcdm());
                    kcNameTree1.setKsldw(cb06Kc.getKsldw());
                    kcNameTree1.setId(cb06Kc.getPkid());


                    //矿石类型筛选下拉
                    kcNameTree1.setCb02KslxList(getKslxList(cb06Kc.getKcdm()));
                    //矿产品级筛选下拉
                    kcNameTree1.setCb03KspjList(kcpjList(cb06Kc.getKcdm()));
                    //矿石质量筛选下拉框
                    kcNameTree1.setCb04KszlList(getKszlList(cb06Kc.getKcdm()));
                }
                kcNameTreeList.add(kcNameTree1);
            }
        }
        return kcNameTreeList;
    }


    /**
     * 矿石类型筛选下拉
     * kcdm 矿产代码
     */
    public List<Cb02Kslx> getKslxList(Integer kcdm) {
        QueryWrapper<Cb02Kslx> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("kcdm", kcdm);
        return cb02KslxMapper.selectList(queryWrapper);
    }


    /**
     * 矿产品级筛选下拉
     * kcdm 矿产代码
     */
    public List<Cb03Kspj> kcpjList(Integer kcdm) {
        QueryWrapper<Cb03Kspj> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("kcdm", kcdm);
        return cb03KspjMapper.selectList(queryWrapper);
    }

    /**
     * 矿石质量筛选下拉框
     * kqdm 矿区代码
     */
    public List<Cb04Kszl> getKszlList(Integer kcdm) {
        QueryWrapper<Cb04Kszl> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("kcdm", kcdm);
        return cb04KszlMapper.selectList(queryWrapper);
    }

    /**
     * 矿产名称下拉框
     */
    private KcNameTree getTreeForKcName() {
        KcNameTree kcNameTree = new KcNameTree();
        kcNameTree.setName("矿产名称");
        kcNameTree.setId("kcmc");
        kcNameTree.setKcdm(0);
        kcNameTree.setKcmcdm("矿产名称");
        kcNameTree.setTreeCode("0_0_0");
        try {
            List<KcNameTree> kcNameTreeList = new ArrayList<>();

            //获取矿类数据列表
            LambdaQueryWrapper<Cb01Zh> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Cb01Zh::getBh, 70);
            queryWrapper.ne(Cb01Zh::getBnm, 0);
            List<Cb01Zh> cb01ZhList = cb01ZhMapper.selectList(queryWrapper);
            //获取矿种列表
            List<Cb06Kc> cb06KcList = cb06KcMapper.selectList(new LambdaQueryWrapper<>());
            //矿石类型列表
            List<Cb02Kslx> cb02KslxList = cb02KslxMapper.selectList(new LambdaQueryWrapper<>());
            //矿产品级列表
            List<Cb03Kspj> cb03KspjList = cb03KspjMapper.selectList(new LambdaQueryWrapper<>());
            //矿石质量列表
            List<Cb04Kszl> cb04KszlList = cb04KszlMapper.selectList(new LambdaQueryWrapper<>());

            //组织二级节点及更深
            for (Cb01Zh cb01Zh : cb01ZhList) {
                //通过矿产代码过滤出矿种列表
                List<Cb06Kc> filterCb06KcList = cb06KcList.stream()
                        .filter(cb06Kc -> cb06Kc.getKclb().equals(String.valueOf(cb01Zh.getDm())))
                        .collect(Collectors.toList());
                //获取矿种名称并去重
                List<String> kcnames = cb06KcList.stream()
                        .filter(cb06Kc -> cb06Kc.getKclb().equals(String.valueOf(cb01Zh.getDm())))
                        .map(Cb06Kc::getKcmc)
                        .distinct()
                        .collect(Collectors.toList());
                //组织节点
                KcNameTree kcNameTree1 = new KcNameTree();
                kcNameTree1.setPid("kcmc");
                kcNameTree1.setId(cb01Zh.getPkid());
                kcNameTree1.setName(cb01Zh.getMc());
                kcNameTree1.setKcdm(cb01Zh.getDm());
                kcNameTree1.setKcmcdm(cb01Zh.getMc());
                kcNameTree1.setTreeCode("1_"+cb01Zh.getDm()+"_0");
                kcNameTree1.setChildren(getKcNameTreeChildren(cb01Zh.getDm(), filterCb06KcList, kcnames, cb02KslxList, cb03KspjList, cb04KszlList));
                kcNameTreeList.add(kcNameTree1);
            }
            kcNameTree.setChildren(kcNameTreeList);
        } catch (Exception e) {
            System.out.println(e);
        }
        return kcNameTree;
    }

    private List<KcNameTree> getKcNameTreeChildren(Integer dm, List<Cb06Kc> cb06KcList, List<String> kcnames, List<Cb02Kslx> cb02KslxList, List<Cb03Kspj> cb03KspjList, List<Cb04Kszl> cb04KszlList) {
        List<KcNameTree> kcNameTreeList = new ArrayList<>();
        for (String kcName : kcnames) {
            //矿产名称过滤出矿种列表
            List<Cb06Kc> filterCb06KcList = cb06KcList.stream()
                    .filter(cb06Kc -> cb06Kc.getKcmc().equals(kcName))
                    .collect(Collectors.toList());
            //组织节点数据
            Cb06Kc cb06Kc = filterCb06KcList.get(0);
            KcNameTree kcNameTree1 = new KcNameTree();
            kcNameTree1.setPid(String.valueOf(dm));
            kcNameTree1.setName(cb06Kc.getKcmc());
            kcNameTree1.setKcdm(cb06Kc.getKcdm());
            kcNameTree1.setKcmcdm(cb06Kc.getKcmc() + " " + cb06Kc.getKcdm());
            kcNameTree1.setKclb(cb06Kc.getKclb());
            kcNameTree1.setTjdx(cb06Kc.getTjdx());
            kcNameTree1.setKsldw(cb06Kc.getKsldw());
            if (cb06Kc.getJsldw() != null){
                kcNameTree1.setJsldw(cb06Kc.getJsldw());
            }
            kcNameTree1.setTreeCode("2_"+cb06Kc.getKcdm()+"_"+cb06Kc.getTjdx());
            if (filterCb06KcList.size() > 1) {
                kcNameTree1.setId(cb06Kc.getKclb());
                List<KcNameTree> kcNameTreeList2 = new ArrayList<>();
                for (Cb06Kc cb06Kc1 : filterCb06KcList) {
                    KcNameTree kcNameTree2 = new KcNameTree();
                    kcNameTree2.setPid(cb06Kc1.getPkid());
                    kcNameTree2.setKcdm(cb06Kc1.getKcdm());
                    kcNameTree2.setKsldw(cb06Kc1.getKsldw());

                    if (cb06Kc1.getJsldw() != null){
                        kcNameTree2.setJsldw(cb06Kc1.getJsldw());
                    }

                    kcNameTree2.setName(cb06Kc1.getJsdxmc() + " " + cb06Kc1.getJsldw());
                    kcNameTree2.setKclb(cb06Kc1.getKclb());
                    kcNameTree2.setTjdx(cb06Kc1.getTjdx());
                    kcNameTree2.setTreeCode("3_"+cb06Kc1.getKcdm()+"_"+cb06Kc1.getTjdx());
                    //矿石类型筛选下拉
                    kcNameTree2.setCb02KslxList(cb02KslxList.stream()
                            .filter(cb02Kslx -> cb02Kslx.getKcdm().equals(cb06Kc1.getKcdm()))
                            .collect(Collectors.toList()));
                    //矿产品级筛选下拉
                    kcNameTree2.setCb03KspjList(cb03KspjList.stream()
                            .filter(cb03Kspj -> cb03Kspj.getKcdm().equals(cb06Kc1.getKcdm()))
                            .collect(Collectors.toList()));
                    //矿石质量筛选下拉框
                    kcNameTree2.setCb04KszlList(cb04KszlList.stream()
                            .filter(cb04Kszl -> cb04Kszl.getKcdm().equals(cb06Kc1.getKcdm()))
                            .collect(Collectors.toList()));
                    kcNameTreeList2.add(kcNameTree2);
                }
                kcNameTree1.setChildren(kcNameTreeList2);
            }
            else{
                kcNameTree1.setKcdm(cb06Kc.getKcdm());
                kcNameTree1.setKsldw(cb06Kc.getKsldw());
                if (cb06Kc.getJsldw() != null){
                    kcNameTree1.setJsldw(cb06Kc.getJsldw());
                }
                kcNameTree1.setId(cb06Kc.getPkid());
                kcNameTree1.setKclb(cb06Kc.getKclb());
                kcNameTree1.setTjdx(cb06Kc.getTjdx());
                kcNameTree1.setTreeCode("2_"+cb06Kc.getKcdm()+"_"+cb06Kc.getTjdx());
                //矿石类型筛选下拉
                kcNameTree1.setCb02KslxList(cb02KslxList.stream()
                        .filter(cb02Kslx -> cb02Kslx.getKcdm().equals(cb06Kc.getKcdm()))
                        .collect(Collectors.toList()));
                //矿产品级筛选下拉
                kcNameTree1.setCb03KspjList(cb03KspjList.stream()
                        .filter(cb03Kspj -> cb03Kspj.getKcdm().equals(cb06Kc.getKcdm()))
                        .collect(Collectors.toList()));
                //矿石质量筛选下拉框
                kcNameTree1.setCb04KszlList(cb04KszlList.stream()
                        .filter(cb04Kszl -> cb04Kszl.getKcdm().equals(cb06Kc.getKcdm()))
                        .collect(Collectors.toList()));
            }
            kcNameTreeList.add(kcNameTree1);
        }
        return kcNameTreeList;
    }

}
